
package com.idealighter.game.games.wknh.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.games.wknh.WknhPlayer;
import com.idealighter.game.games.wknh.dto.BatteryInfo;
import com.idealighter.game.games.wknh.message.ReqExchangeAllChipsMsg;
import com.idealighter.game.games.wknh.message.ResScenceMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.common.PlayerPosition;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(523237)
public class ResScenceHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResScenceHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    ResScenceMsg msg = (ResScenceMsg) message;
    player.position = PlayerPosition.TABLE;
    LOG.info("[悟空闹海]玩家[{}][{}]进入桌子[{}]成功", player.userName, player.playerId,
        player.table(player.playerId).getId());

    WknhPlayer wknhPlayer = (WknhPlayer) player;
    for (BatteryInfo battery : msg.getBatterys()) {
      if (battery.getPlayerId() == wknhPlayer.playerId) {
        wknhPlayer.battery = battery;
        break;
      }
    }
    // 随机延时3到5秒兑换筹码
    wknhPlayer.schedule(() -> {
      exchangeAllChips(player);
      wknhPlayer.scheduleFire();
    }, RandCodeUtil.random(3, 5), TimeUnit.SECONDS);
  }

  /**
   * 进入桌子后兑换全部金币为筹码,然后马上准备.
   * 
   * @param player .
   */
  private void exchangeAllChips(Player player) {
    ReqExchangeAllChipsMsg msg = new ReqExchangeAllChipsMsg();
    player.sendMsg(msg);
  }
}
